Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

467
Vistas
How to update flatlist arrays in functional component [extraData won't work]

After removing an array object I expect the flatlist to update but it fails until I reload the app. Is there a way to get the update in real time?

My code:

    ...imports...

arr = [
  {id:1, name:'test1'}, 
  {id:2, name:'test2'},
  {id:3, name:'test3'},
]
export default function App() {
  const [posts, setPosts] = useState([])

  setTimeout(() => { delBet() }, 8000)
  function delBet() {
    try {
      arr.splice(arr[2], 1); setPosts(arr)
      //only remove {id:3, name:'test3'},

      console.log(arr) //works well
      //only show  [{id:1, name:'test1'},{id:2, name:'test2'}]
    } catch (e) { console.log(e) }
  }
  return (
    <FlatList
      data={posts}
      extraData={posts} //doesn't work/make any difference
      renderItem={({ item }) =>
        <Post
          post={item}
        />
      }
    />
  )
}

I'd appreciate any kind of help (including improving my code)

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your state is mutated,

try

setPosts([...arr]); instead of setPosts(arr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

The actual value of posts is [], if you need try to initialize the posts with the arr value, Just like this:

const [posts, setPosts] = useState(arr)

Additionally you can use setTimeout inside a use effect for render just once.

useEffect(()=> { 
  setTimeout(() => { delBet() }, 8000)
  function delBet() {
   try {
     setPosts(() => arr.splice(arr[2], 1))
   } catch (e) { console.log(e) }
  }
}, [])
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda